package com.jstyle.nordic_otas.nordic_otas_plugin;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.provider.Settings;
import android.text.TextUtils;

import androidx.annotation.NonNull;

public class NoticeUtils {

    private static final String ENABLED_NOTIFICATION_LISTENERS = "enabled_notification_listeners";
    public static boolean isEnabledNotification(Context context) {
        String pkgName = context.getPackageName();
        final String flat = Settings.Secure.getString(context.getContentResolver(), ENABLED_NOTIFICATION_LISTENERS);
        if (!TextUtils.isEmpty(flat)) {
            final String[] names = flat.split(":");
            for (int i = 0; i < names.length; i++) {
                final ComponentName cn = ComponentName.unflattenFromString(names[i]);
                if (cn != null) {
                    if (TextUtils.equals(pkgName, cn.getPackageName())) {
                        return true;
                    }
                }
            }
        }
        return false;
    }


    /**
     * Mac尾数加法
     *
     * @param mac Mac地址：xx:xx:xx.....
     * @return 返回为数加 加后的Mac地址
     * @author YOLANDA
     */
    public static String getMacAdd(@NonNull String mac) {
        final String firstBytes = mac.substring(0, 15);
        final String lastByte = mac.substring(15); // assuming that the device address is correct
        final String lastByteIncremented = String.format("%02X", (Integer.valueOf(lastByte, 16) + 1) & 0xFF);
        return (firstBytes + lastByteIncremented).toUpperCase();
    }

    /**
     * Mac尾数减法
     *
     * @param mac Mac地址：xx:xx:xx.....
     * @return 返回为数加 减后的Mac地址
     * @author YOLANDA
     */
    public static String getMacMinus(@NonNull String mac) {
        final String firstBytes = mac.substring(0, 15);
        final String lastByte = mac.substring(15); // assuming that the device address is correct
        final String lastByteIncremented = String.format("%02X", (Integer.valueOf(lastByte, 16) - 1) & 0xFF);
        return (firstBytes + lastByteIncremented).toUpperCase();
    }

    public static void setting(Context context){
        context.startActivity(new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS));
    }
}
